package com.hxkj.service.wechat.impl;


import com.hxkj.client.service.AppletAgentServiceClient;
import com.hxkj.client.service.AppletServiceClient;
import com.hxkj.client.service.AuthUserServiceClient;
import com.hxkj.domain.enums.AppletAgentExceptionEnum;
import com.hxkj.domain.po.*;
import com.hxkj.service.wechat.AppletAgentService;
import com.hxkj.token.utils.TokenUtils;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 用户mapper
 * <p>
 * Created by chenfangzhou on 2018/5/15.
 */
@Service
@Slf4j
public class AppletAgentServiceImpl implements AppletAgentService {


    @Autowired
    private AppletAgentServiceClient appletAgentServiceClient;

    @Autowired
    private AuthUserServiceClient authUserServiceClient;

    @Autowired
    private AppletServiceClient appletServiceClient;
    @Autowired
    private TokenUtils<AuthUser> tokenUtils;

    /**
     * 给用户关联自媒体业务员角色
     *
     * @param appletAgent
     * @return
     * @throws Exception
     */
    @Override
    public ResultMessageStruct addAppletToUser(AppletAgent appletAgent) throws Exception {
        if (appletAgent == null) {
            throw new CustomException(AppletAgentExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            result = appletAgentServiceClient.addAppletToUser(appletAgent);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(AppletAgentExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(AppletAgentExceptionEnum.getEnumByCode(result.getCode()));
        }

    }

    @Override
    public ResultMessageStruct removeAppletToUser(AppletAgent appletAgent) throws Exception {
        if (appletAgent == null) {
            throw new CustomException(AppletAgentExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            result = appletAgentServiceClient.removeAppletToUser(appletAgent);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(AppletAgentExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(AppletAgentExceptionEnum.getEnumByCode(result.getCode()));
        }
    }

    /**
     * 获取自媒体业务员列表
     *
     * @param
     * @return
     * @throws Exception
     */
    @Override
    public ResultPageStruct<AuthUserAgent> getAgentList(AuthUserAgent authUserAgent, PageTo pageParam) throws Exception {
        if (authUserAgent == null) {
            throw new CustomException(AppletAgentExceptionEnum.PARAMETER_IS_NULL);
        }
        //将前端传过来的文章创建结束时间 + 一天减一秒
        if(authUserAgent.getCreateTimeEnd()!=null){
            authUserAgent.setCreateTimeEnd(authUserAgent.getCreateTimeEnd() + 24 * 60 * 60 * 1000 - 1);
        }
        ResultPageStruct<AuthUserAgent> result = new ResultPageStruct<>();
        ParameterDataStruct<AuthUserAgent> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(authUserAgent);
        parameterDataStruct.setPageTo(pageParam);
        //获取所有分配了自媒体角色的用户id集合
        result = authUserServiceClient.getAgentList(parameterDataStruct);
        result.setCode(null);
        result.setMessage(null);
        return result;
    }

    /**
     * 获取指定业务员已经分配的小程序
     *
     * @param appletAgent
     * @return
     * @throws Exception
     */
    @Override
    public ResultPageStruct<Applet> getAppletList(AppletAgent appletAgent, PageTo pageParam) throws Exception {
        ResultPageStruct<Applet> result = new ResultPageStruct<>();
        ParameterDataStruct<AppletAgent> parameterDataStruct = new ParameterDataStruct<>();
        //查询指定业务员已经关联的小程序,给定agentId和siteId
        parameterDataStruct.setData(appletAgent);
        parameterDataStruct.setPageTo(pageParam);
        if (appletAgent.getAgentId() == null){
            appletAgent.setAgentId(tokenUtils.getTokenUserInfo().getUserId());
        }
        //0表示前台需要获取的是业务员已关联的小程序
        if ("0".equals(appletAgent.getStatus())) {
            result = appletAgentServiceClient.getAssignAppletList(parameterDataStruct);
        } else {
            //1表示未关联
            result = appletAgentServiceClient.getNoAssignAppletList(parameterDataStruct);
        }
        result.setCode(null);
        result.setMessage(null);
        return result;
    }

    @Override
    public ResultMessageStruct saveAppletArticle(AppletAgent appletAgent) throws Exception {
        if (appletAgent == null) {
            throw new CustomException(AppletAgentExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            result = appletAgentServiceClient.saveAppletArticle(appletAgent);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(AppletAgentExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

    }

}
